<?php
/**
 *   Author: Yanlongli <jobs@yanlongli.com>
 *   Date:   2019/11/9
 *   IDE:    PhpStorm
 *   Desc:  抓取规则实现
 */

namespace yanlongli\temporaryMailboxGrab;

define('HDOM_TYPE_ELEMENT', 1);
define('HDOM_TYPE_COMMENT', 2);
define('HDOM_TYPE_TEXT', 3);
define('HDOM_TYPE_ENDTAG', 4);
define('HDOM_TYPE_ROOT', 5);
define('HDOM_TYPE_UNKNOWN', 6);
define('HDOM_QUOTE_DOUBLE', 0);
define('HDOM_QUOTE_SINGLE', 1);
define('HDOM_QUOTE_NO', 3);
define('HDOM_INFO_BEGIN', 0);
define('HDOM_INFO_END', 1);
define('HDOM_INFO_QUOTE', 2);
define('HDOM_INFO_SPACE', 3);
define('HDOM_INFO_TEXT', 4);
define('HDOM_INFO_INNER', 5);
define('HDOM_INFO_OUTER', 6);
define('HDOM_INFO_ENDSPACE', 7);

defined('DEFAULT_TARGET_CHARSET') || define('DEFAULT_TARGET_CHARSET', 'UTF-8');
defined('DEFAULT_BR_TEXT') || define('DEFAULT_BR_TEXT', "\r\n");
defined('DEFAULT_SPAN_TEXT') || define('DEFAULT_SPAN_TEXT', ' ');
defined('MAX_FILE_SIZE') || define('MAX_FILE_SIZE', 600000);
define('HDOM_SMARTY_AS_TEXT', 1);


class simpleTempMailBox
{


    /**
     * @param       $domains
     * @param       $useCache
     * @param string $cachePath
     * @param array $errList
     * @return array
     */
    public static function grasp($domains, $useCache, $cachePath = '', &$errList = [])
    {


        $arr = [];
        foreach ($domains as $domain => $rule) {
            $_emailBoxList = [];
            //是否根据节点取列表
            if (isset($rule['el']) && $rule['el']) {

                //将域名进行小写转换和去除空白符号
                $domain = strtolower(trim($domain));

                //爬虫
                $url = $domain;
                if (file_exists($cachePath . md5($domain)) && $useCache) {
                    $url = $cachePath . md5($domain);
                } else {

                    if (isset($rule['https']) && $rule['https']) {
                        $url = 'https://' . $domain;
                    } else {
                        $url = 'http://' . $domain;
                    }
                    if (isset($rule['path']) && $rule['path']) {
                        $url .= $rule['path'];
                    }

                }
                //抓取url
                $html = simpleHtmlDom::file_get_html($url);
                //抓取结果
                if (!$html) {
                    //抓取失败记录
                    $errList[] = $domain;
                    continue;
                }

                //写缓存
                file_put_contents($cachePath . md5($domain), $html->getDoc());


                //遍历列表
                foreach ($html->find($rule['el']) as $key => $element) {

                    if (isset($rule['notKey']) && is_array($rule['notKey']) && in_array($key, $rule['notKey'])) {
                        continue;
                    }

                    //取值
                    if (isset($rule['value']) && $rule['value']) {
                        //从value取值
                        $_emailDomain = $element->value;
                    } else {
                        //从text取值
                        $_emailDomain = $element->innertext;
                    }

                    //空白字符串 不處理
                    if (!trim($_emailDomain)) {
                        continue;
                    }

                    //unicode 转码 &#XXXX;
                    if (isset($rule['unicode']) && $rule['unicode']) {
                        $_emailDomain = html_entity_decode($_emailDomain);
                    }

                    //截取 @ 符号
                    if (isset($rule['@']) && $rule['@']) {
                        $_index = strpos($_emailDomain, '@');
                        if (is_numeric($_index)) {
                            $_emailDomain = substr($_emailDomain, $_index + 1);
                        }
                    }


                    if (isset($rule['space']) && $rule['space']) {
                        $_index = strpos($_emailDomain, ' ');
                        if (is_numeric($_index)) {
                            $_emailDomain = substr($_emailDomain, 0, $_index);
                        }
                    }


                    if (isset($rule['callable'])) {
                        $_emailDomain = call_user_func($rule['callable'], $_emailDomain);
                    }

                    //空白字符串 不處理
                    if (!trim($_emailDomain)) {
                        continue;
                    }

                    //清除两边空白
                    $_emailDomain = trim($_emailDomain);

                    $_emailBoxList[] = $_emailDomain;


                    continue;
                }
            }

            if (isset($rule['list'])) {
                foreach ($rule['list'] as $_emailDomain) {
                    $_emailBoxList[] = $_emailDomain;
                }
            }
            $_emailBoxList = array_values(array_unique($_emailBoxList));
            $arr[$domain] = $_emailBoxList;
        }

        return $arr;

    }
}